use crate::Stack;

pub fn paren_check(paren: &str) -> bool {
    let mut char_list = Vec::new();
    for c in paren.chars() {
        char_list.push(c);
    }
    let mut index = 0;
    let mut balance = true;
    let mut stack = Stack::new();

    while index < char_list.len() && balance {
        let c = char_list[index];
        if '(' == c {
            //如果为开始括号入栈
            stack.push(c);
        } else {
            //如果为结束符号，判断栈是否为空
            if stack.is_empty() {
                //栈为空，所以括号不匹配
                balance = false
            } else {
                let _r = stack.pop();
            }
        }
        index += 1;
    }
    //仅当平衡且栈为空是括号才是匹配的
    balance && stack.is_empty()
}
